package mail_operations;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import GUIMain.GUILoader;

import data.ConfigInfo;
import data.EmployeeInfo;
import data.ProfileList;
import fileIO.XLSWriter;

public class ReportSaveAndMail {
	private static HashMap<String,ArrayList<String>> getMailingListForOffice(String office,ArrayList<String> cneeList,boolean origin){
		HashMap<String,ArrayList<String>> mailingMap = new HashMap<String,ArrayList<String>>();
		for(String cnee:cneeList){
			mailingMap.put(office, ProfileList.profileList.get(cnee.toUpperCase()).getEmployees(origin).get(office));
		}
		return mailingMap;
	}
	public static ArrayList<String> saveAndMailToOrigin(HSSFWorkbook wkb,ArrayList<String> cneeList, String office,String filePath, String origin, boolean email){
		HashMap<String,ArrayList<String>> mailingList = new HashMap<String,ArrayList<String>>(); //Hashmap of CNEE, arraylist of employees
		File file = XLSWriter.write(wkb, filePath +"\\"+ office +"_"+origin+ GUILoader.getClock().getTime("MM_dd_yy_Hms")+ ".xls");
		ArrayList<String> errorList = new ArrayList<String>();
		
		if(origin.equals("ORIGIN")){
			if(file==null){
				errorList.add("File cannot be saved! - Path " + filePath + "\\" + office + ".xls");
				return errorList;
			}
			//Iterate through all CNEE lists to 
			mailingList = getMailingListForOffice(office,cneeList,true);

		}else if(origin.equals("DEST")){
			if(file==null){
				errorList.add("File cannot be saved! - Path " + filePath + "\\" + office + ".xls");
				return errorList;
			}
			mailingList = getMailingListForOffice(office,cneeList,false);
		}
		if(email && file!=null){

			try{
//			   	//Parse emails to copy
				ArrayList<InternetAddress> ccArray = new ArrayList<InternetAddress>();
				if(ConfigInfo.SEND_TO_SELF){
					ccArray.add(InternetAddress.parse(ConfigInfo.SENDER_EMAIL)[0]);
				}
				if(!ConfigInfo.ADDITIONAL_EMAIL_LIST.isEmpty()){
					for(int i=0;i<ConfigInfo.ADDITIONAL_EMAIL_LIST.size();i++){
						ccArray.add(InternetAddress.parse(ConfigInfo.ADDITIONAL_EMAIL_LIST.get(i))[0]);
					}
				}
				//key set of cnee names
				Object[] keySet = mailingList.keySet().toArray();
				for(int z=0;z<keySet.length;z++){
					if(mailingList.get(keySet[z]) != null){
						for(int i=0;i<mailingList.get(keySet[z]).size();i++){
							String subject = MailerGUI.getSubject(mailingList.get(keySet[z]).get(i),(String)keySet[z]);
							String body = MailerGUI.getBody(mailingList.get(keySet[z]).get(i),(String)keySet[z]);
							//Mail to each employee and copy list
							ArrayList<String> errorListTemp  = mail(EmployeeInfo.getEmployeeEmail((String)keySet[z],mailingList.get(keySet[z]).get(i)),
									ccArray.toArray(),subject, body , file);
							for(int x =0;x<errorListTemp.size();x++){
								if(!errorList.contains(errorListTemp.get(x)))
									errorList.add(errorListTemp.get(x));
							}
						}
					}
				}
				//Release file from memory
				file = null;
				return errorList;
			}catch (AddressException e) {
			}
		}
		file = null;
		return errorList;
	}	
	public static ArrayList<String> saveAndMail(HSSFWorkbook wkb, String cnee,String filePath, String origin, boolean email){
		HashMap<String,ArrayList<String>> mailingList = new HashMap<String,ArrayList<String>>();
		File file = XLSWriter.write(wkb, filePath +"\\"+ cnee +"_"+origin+ GUILoader.getClock().getTime("MM_dd_yy_Hms")+ ".xls");
		ArrayList<String> errorList = new ArrayList<String>();
		
		if(origin.equals("ORIGIN")){
			if(file==null){
				errorList.add("File cannot be saved! - Path " + filePath + "\\" + cnee + ".xls");
				return errorList;
			}
			mailingList = ProfileList.profileList.get(cnee.toUpperCase()).getEmployees(true);

		}else if(origin.equals("DEST")){
			if(file==null){
				errorList.add("File cannot be saved! - Path " + filePath + "\\" + cnee + ".xls");
				return errorList;
			}
			mailingList = ProfileList.profileList.get(cnee.toUpperCase()).getEmployees(false);
		}
		if(email && file!=null){

			try{
//			   	//Parse emails to copy
				ArrayList<InternetAddress> ccArray = new ArrayList<InternetAddress>();
				if(ConfigInfo.SEND_TO_SELF){
					ccArray.add(InternetAddress.parse(ConfigInfo.SENDER_EMAIL)[0]);
				}
				if(!ConfigInfo.ADDITIONAL_EMAIL_LIST.isEmpty()){
					for(int i=0;i<ConfigInfo.ADDITIONAL_EMAIL_LIST.size();i++){
						ccArray.add(InternetAddress.parse(ConfigInfo.ADDITIONAL_EMAIL_LIST.get(i))[0]);
					}
				}
				//Get list of employees
				Object[] keySet = mailingList.keySet().toArray();
				for(int z=0;z<keySet.length;z++){
					for(int i=0;i<mailingList.get(keySet[z]).size();i++){
						String subject = MailerGUI.getSubject(mailingList.get(keySet[z]).get(i),cnee);
						String body = MailerGUI.getBody(mailingList.get(keySet[z]).get(i),cnee);
						//Mail to each employee and copy list
						ArrayList<String> errorListTemp  = mail(EmployeeInfo.getEmployeeEmail((String)keySet[z],mailingList.get(keySet[z]).get(i)),
								ccArray.toArray(),subject, body , file);
						for(int x =0;x<errorListTemp.size();x++){
							if(!errorList.contains(errorListTemp.get(x)))
								errorList.add(errorListTemp.get(x));
						}
					}
				}
				//Release file from memory
				file = null;
				return errorList;
			}catch (AddressException e) {
			}
		}
		file = null;
		return errorList;
	}
	public static ArrayList<String> mail(String receiver, Object[] ccList, String subject, String body, File fileName){
		
		//temp fields for testing
		return ReportMailer.mail(ConfigInfo.SERVER_ADDRESS, ConfigInfo.SENDER_EMAIL, 
				ccList, receiver, subject, body, fileName);
	}
}
